import { Pipe, PipeTransform } from '@angular/core';
// im聊天-时间过滤
@Pipe({
    name: 'imDateFilter',
})
export class ImDateFilter implements PipeTransform {
    transform(value: any, args?: any, type?: any): any {
        if (value === undefined || value === null || value === '') {
            return '';
        }
        try {
            // 消息时间
            // 今天内
            // 时:分（示例：15:06）
            // 昨天内
            // 昨天 时:分（示例：昨天 15:06）
            // 最近一周内
            // 星期几 时:分（示例：星期五 15:06）
            // 今年内
            // 月-日 时:分（示例：08-08 15:06）
            // 去年以前
            // 年-月-日 时:分（示例：2014-08-08 15:06）
            value = value.replace(/\-/g, "/");
            const nowDate = new Date();
            const prettyDate = new Date(value.replace(/\-/g, "/"));
            const nowTime = new Date().getTime() / 1000;
            const prettyTime = prettyDate.getTime() / 1000;
            const diff = nowTime - prettyTime;
            if (this.isValidDate(prettyDate)) {
                if (diff >= 0 && diff < 300) {        // 刚刚：5分钟之内
                    const argStr = String(args);
                    if (argStr === '') {
                        return this.formatDate(prettyDate, "hh:mm");
                    } else {
                        return '';
                    }
                } else if (prettyDate.getTime() > this.beginOfToday() && diff < 24 * 60 * 60) { // 小时前：一天之内
                    return this.formatDate(prettyDate, "hh:mm");
                } else if (prettyDate.getTime() > this.beginOfYesterday() && prettyDate.getTime() < this.beginOfToday()) {
                    return "昨天 " + this.formatDate(prettyDate, "hh:mm");
                } else if (prettyDate.getTime() > this.beginOfThisWeek() && prettyDate.getTime() < this.beginOfYesterday()) {// 一周内
                    const week = new Array("日", "一", "二", "三", "四", "五", "六");
                    return "星期" + week[new Date(prettyDate).getDay()] + " " + this.formatDate(prettyDate, "hh:mm");
                } else if (nowDate.getFullYear() === prettyDate.getFullYear()) {// 具体日期：超过一个月，本年度内
                    return this.formatDate(prettyDate, "MM-dd hh:mm");
                } else { // 具体日期：去年
                    return this.formatDate(prettyDate, "yyyy-MM-dd hh:mm");
                }
            } else {
                return this.formatDate(new Date(), "hh:mm");
            }
        } catch (error) {
            return this.formatDate(new Date(), "hh:mm");
        }

    }


    beginOfThisWeek() {
        const mydate = new Date();
        return new Date(this.beginOfToday()).getTime() - (mydate.getDay()) * (24 * 60 * 60 * 1000);
    }

    beginOfToday() {
        const mydate = new Date();
        let myyear, mymonth, myday, mytime;
        myyear = mydate.getFullYear();
        mymonth = (mydate.getMonth() + 1) < 10 ? '0' + (mydate.getMonth() + 1) : mydate.getMonth() + 1;
        myday = mydate.getDate() < 10 ? '0' + mydate.getDate() : mydate.getDate();
        mytime = myyear + "-" + mymonth + "-" + myday + " 00:00:00";
        return (new Date(mytime).getTime());
    }

    beginOfYesterday() {
        return new Date(this.beginOfToday()).getTime() - (24 * 60 * 60 * 1000);
    }

    formatDate = (dateToFormat, format) => {
        const date = {
            "M+": dateToFormat.getMonth() + 1,
            "d+": dateToFormat.getDate(),
            "h+": dateToFormat.getHours(),
            "m+": dateToFormat.getMinutes(),
            "s+": dateToFormat.getSeconds(),
            "q+": Math.floor((dateToFormat.getMonth() + 3) / 3),
            "S+": dateToFormat.getMilliseconds()
        };
        if (/(y+)/i.test(format)) {
            format = format.replace(RegExp.$1, (dateToFormat.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        for (const k in date) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
            }
        }
        return format;
    }


    isValidDate = (prettyDate) => {
        return prettyDate instanceof Date && !isNaN(prettyDate.getTime());
    }

}
